﻿using System;
using System.IO.Ports;
using System.Windows.Forms;
using Modbus.Device;

namespace HenhouseEnvironmentCtrl
{
    public partial class EquipmentCtrlForm : Form
    {
        private IModbusSerialMaster master;
        private SerialPort port;
        private int currentSlaveId = 1;
        private System.Threading.Timer refreshTimer;

        public EquipmentCtrlForm(IModbusSerialMaster master, SerialPort port)
        {
            InitializeComponent();
            this.master = master;
            this.port = port;
        }

        private void EquipmentCtrlForm_Load(object sender, EventArgs e)
        {
            LoadDeviceStatus();
            refreshTimer = new System.Threading.Timer(RefreshDeviceStatus, null, 0, 5000);
        }

        private void LoadDeviceStatus()
        {
            try
            {
                ushort[] registers = master.ReadHoldingRegisters((byte)currentSlaveId, 5, 5);
                lblFan1Status.Text = registers[0] == 1 ? "开" : "关";
                lblFan2Status.Text = registers[1] == 1 ? "开" : "关";
                lblWetCurtainStatus.Text = registers[2] == 1 ? "开" : "关";
                lblLightStatus.Text = registers[3] == 1 ? "开" : "关";
                lblRadiatorStatus.Text = registers[4] == 1 ? "开" : "关";
            }
            catch (Exception ex)
            {
                MessageBox.Show("读取数据失败: " + ex.Message);
            }
        }

        private void RefreshDeviceStatus(object state)
        {
            this.Invoke(new Action(() => LoadDeviceStatus()));
        }

        private void ToggleDevice(int registerIndex, Label statusLabel, float tempChange, float humidityChange)
        {
            try
            {
                ushort[] registers = master.ReadHoldingRegisters((byte)currentSlaveId, 5, 5);
                ushort newValue = (ushort)(registers[registerIndex] == 1 ? 0 : 1);
                master.WriteSingleRegister((byte)currentSlaveId, (ushort)(5 + registerIndex), newValue);
                statusLabel.Text = newValue == 1 ? "开" : "关";

                if (newValue == 1)
                {
                    AdjustEnvironment(tempChange, humidityChange);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("操作失败: " + ex.Message);
            }
        }

        private void AdjustEnvironment(float tempChange, float humidityChange)
        {
            try
            {
                ushort[] environmentData = master.ReadHoldingRegisters((byte)currentSlaveId, 0, 2);
                float currentTemp = environmentData[0] / 10.0f;
                float currentHumidity = environmentData[1] / 10.0f;

                currentTemp += tempChange;
                currentHumidity += humidityChange;

                master.WriteSingleRegister((byte)currentSlaveId, 0, (ushort)(currentTemp * 10));
                master.WriteSingleRegister((byte)currentSlaveId, 1, (ushort)(currentHumidity * 10));
            }
            catch (Exception ex)
            {
                MessageBox.Show("环境调整失败: " + ex.Message);
            }
        }

        private void BtnFan1_Click(object sender, EventArgs e)
        {
            ToggleDevice(0, lblFan1Status, -0.5f, 0.0f);
        }

        private void BtnFan2_Click(object sender, EventArgs e)
        {
            ToggleDevice(1, lblFan2Status, -0.5f, 0.0f);
        }

        private void BtnWetCurtain_Click(object sender, EventArgs e)
        {
            ToggleDevice(2, lblWetCurtainStatus, -0.5f, 2.0f);
        }

        private void BtnLight_Click(object sender, EventArgs e)
        {
            ToggleDevice(3, lblLightStatus, 1.0f, 0.0f);
        }

        private void BtnRadiator_Click(object sender, EventArgs e)
        {
            ToggleDevice(4, lblRadiatorStatus, 1.0f, 0.0f);
        }

        private void BtnBack_Click(object sender, EventArgs e)
        {
            refreshTimer.Dispose();
            this.Close();
        }

        private void BtnSlave1_Click(object sender, EventArgs e)
        {
            currentSlaveId = 1;
            LoadDeviceStatus();
        }

        private void BtnSlave2_Click(object sender, EventArgs e)
        {
            currentSlaveId = 2;
            LoadDeviceStatus();
        }

        private void BtnSlave3_Click(object sender, EventArgs e)
        {
            currentSlaveId = 3;
            LoadDeviceStatus();
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            refreshTimer.Dispose();
        }


    }
}
